package com.bnlr.lrparking.appui.paypwd;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.event.SetPayPasswordEvent;
import com.bnlr.lrparking.model.BaseArrayBean;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.util.pwddialog.SecurityEditCompileListener;
import com.bnlr.lrparking.util.pwddialog.SecurityPasswordEditText;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 修改支付密码
 */
public class AlterPayPswActivity extends BaseActivity {
    private Unbinder unbinder;
    @BindView(R.id.alter_pay_psw)
    SecurityPasswordEditText psw;
    private String password;
    @BindView(R.id.tv_nav_title)
    TextView tvTitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alter_pay_psw);
        unbinder = ButterKnife.bind(this);
        tvTitle.setText("修改支付密码");
        EventBus.getDefault().register(this);//
        psw.getEditText(this);
        psw.setSecurityEditCompileListener(new SecurityEditCompileListener() {
            @Override
            public void onNumCompleted(String num) {
                password = num;
            }
        });
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void SuccessEvent(SetPayPasswordEvent event){
        finish();
    }

    @OnClick({R.id.forget_pay_psw,R.id.tv_alter_next})
    public void alterOnClick (View view) {
        switch (view.getId()) {
            case R.id.forget_pay_psw:
                Intent intent1 = new Intent(this,SetPayPasswordActivity.class);
                intent1.putExtra("reset","reset");
                startActivity(intent1);
                break;
            case R.id.tv_alter_next:
                if (psw.isEditTextEmpty()) {
                    ToastUtils.showShort("请输入原支付密码");
                } else if (!psw.isComplete()){
                    ToastUtils.showShort("密码格式不正确");
                }else {
                    verifyPayPsw(password);
                }
                break;
        }
    }

    /**
     * 校验支付密码是否正确
     * @param password
     */
    private void verifyPayPsw(final String password) {
        try {
            mRequest = NoHttp.createStringRequest(Urls.NEW_IP, LrConstant.POST);
            mRequest.add("action", "ValidUserPayPwd");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("payPassword", password);
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<BaseArrayBean>(baseContext, true, BaseArrayBean.class) {
                        @Override
                        public void doWork(BaseArrayBean data, String code) {
                            Intent intent=new Intent(AlterPayPswActivity.this,PayPasswordActivity.class);
                            intent.putExtra("phonepwd",password);
                            intent.putExtra("tag","alter");//设置支付密码
                            startActivity(intent);
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            if (!code.equals("1")) {
                                try {
                                    psw.clearEdit();
                                    ToastUtils.showShort(obj.getString("msg"));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }, true);
        } catch (Exception e) {

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
        EventBus.getDefault().unregister(this);
    }
}
